<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

if(isset($_SESSION['u_nic'])&&isset($_GET['id'])) {
	$tpl->assign(array(
		'iconesleft' => 2,
		'prod' => '0',
		'prod_id' => intval($_GET['id'])
	));
	include('php/lib/panel_inc.php');
	$heberg_current = intval($_GET['id']);
	$infos_heberg = mysql_query("SELECT comptes_heberg.id AS heberg_id,
										comptes_heberg.u_nic,
										comptes_heberg.uid_linux,
										comptes_heberg.user_linux,
										comptes_heberg.offre,
										comptes_heberg.etat,
										comptes_heberg.mails AS c_bal,
										comptes_heberg.alias_mails AS c_a_mails,
										comptes_heberg.dom_lie,
										comptes_domain.domaine,
										comptes_domain.id,
										offre_heberg.comptes_mails AS o_bal_,
										offre_surmesure.comptes_mails as o_bal,
										offre_heberg.alias_mails AS o_a_mails_,
										offre_surmesure.alias_mails as o_a_mails
										FROM comptes_heberg 
										LEFT JOIN offre_heberg
										ON comptes_heberg.offre = offre_heberg.id_offre
										LEFT JOIN comptes_domain
										ON comptes_heberg.dom_lie = comptes_domain.id
										LEFT JOIN offre_surmesure
										ON comptes_heberg.id = offre_surmesure.id_heberg
										WHERE comptes_heberg.id='".$heberg_current."' AND comptes_heberg.u_nic='".$_SESSION['u_nic']."'")or die(mysql_error());
	if(mysql_num_rows($infos_heberg) == 1) {
		$infos_heberg = mysql_fetch_array($infos_heberg);
		if($infos_heberg['o_bal'] == "") {
			$infos_heberg['o_bal'] = $infos_heberg['o_bal_'];
			$infos_heberg['o_a_mails'] = $infos_heberg['o_a_mails_'];
		}
		$tpl->assign(array('etat' => $infos_heberg['etat']));
		if($infos_heberg['etat'] == 1) {
			if($_GET['action'] == "add_bal") {
				if($infos_heberg['c_bal'] < $infos_heberg['o_bal']) {
					if(isset($_POST['mail_user'])) {
						$error = 0;
						$domaine = intval($_POST['mail_domain']);
						if($_POST['password1'] == $_POST['password2']) {
							if($domaine == 0) {
								if($infos_heberg['dom_lie'] != 0) {
									$mail_dom = $infos_heberg['domaine'];
								}
								else {
									$mail_dom = $infos_heberg['user_linux'].".breizh-web.net";
								}
							}
							else {
								$domaine_pro = query("SELECT * FROM hebergdata_doms WHERE id = '".$domaine."' AND u_nic = '".$_SESSION['u_nic']."'");
								if(mysql_num_rows($domaine_pro) == 1) {
									$domaine_pro = mysql_fetch_array($domaine_pro);
									if($domaine_pro['id_alias_bh'] == 0) {
										$mail_dom = $domaine_pro['domaine'];
									}
									else {
										$mail_dom = $domaine_pro['domaine'].".".$alias_bh[$domaine_pro['id_alias_bh']];
									}
								}
								else {
									$error = 2;
								}
							}
							if($mail_dom != "") {
								$password = mysql_real_escape_string($_POST['password1']);
								$adresse_mail = preg_replace('#([^a-z0-9\-\_/])+#i', '', strtolower($_POST['mail_user']))."@".$mail_dom;
								$mail_exi = mysql_fetch_array(query("SELECT COUNT(*) AS exi FROM hebergdata_bal WHERE email = '".$adresse_mail."'"));
								if($mail_exi['exi'] == 0) {
									query("INSERT INTO hebergdata_bal(email, password, u_nic, id_heberg) VALUES('".$adresse_mail."', ENCRYPT('".$password."'), '".$_SESSION['u_nic']."', '".$infos_heberg['heberg_id']."')");
									query("UPDATE comptes_heberg SET mails = mails + 1 WHERE id  = '".$infos_heberg['heberg_id']."'");
									$deja_dom = mysql_fetch_array(query("SELECT COUNT(*) AS deja FROM hebergdata_postfix_doms WHERE domain = '$mail_dom'"));
									if($deja_dom['deja'] == 0) {
										query("INSERT INTO hebergdata_postfix_doms(domain) VALUES('$mail_dom')");
									
									}
									$error = 3;
								}
								else {
									$error = 2;
								}
							}
						
						}
						else {
							$error = 1;
						}
						
						
					}
					
					$parse = 1;
					$title = "Ajouter une boite mail";
					$tplinc = "panel-hebergement-mails-addbal";
					$liste_dom = query("SELECT * FROM hebergdata_doms WHERE u_nic = '".$_SESSION['u_nic']."' AND id != '".$infos_heberg['dom_lie']."'");
					While($doms = mysql_fetch_array($liste_dom)) {
						if($doms['id_alias_bh'] == 0) {
							$tpl->assign_array('liste_dom', array(
								'id' => $doms['id'],
								'domaine' => $doms['domaine'],
							));
						}
						else {
							$tpl->assign_array('liste_ad', array(
								'id' => $doms['id'],
								'domaine' => $doms['domaine'].'.'.$alias_bh[$doms['id_alias_bh']],
							));
						}
					}
					if($infos_heberg['dom_lie'] != 0) {
						$tpl->assign(array('domaine_principal' => $infos_heberg['domaine']));
					}
					else {
						$tpl->assign(array('domaine_principal' => $infos_heberg['user_linux'].".breizh-web.net"));
					}
					if(isset($error)) {
						$tpl->assign(array(
							"load" => "new Effect.Pulsate(document.getElementById('error'))",
							"error" => $error
						));
					}
				}
				else {
					header("Location:/panel/hebergement-mails.html?id=".intval($_GET['id']));
				}
			}
			elseif($_GET['action'] == "pass_bal") {
				if(isset($_GET['bal'])) {
					$proprietes = query("SELECT * FROM hebergdata_bal WHERE id ='".intval($_GET['bal'])."' AND u_nic = '".$_SESSION['u_nic']."'");
					if(mysql_num_rows($proprietes) == 1) {
						$proprietes = mysql_fetch_array($proprietes);
						$parse = 1;
						$title = "Modifier le mot de passe d'une boite mail";
						$tplinc = "panel-hebergement-mails-pass_bal";
						$tpl->assign(array(
							'bal' => $proprietes['email']
						));
						if(isset($_POST['password1'])) {
							if($_POST['password1'] == $_POST['password2']) {
								$password = mysql_real_escape_string($_POST['password1']);
								query("UPDATE hebergdata_bal SET password = ENCRYPT('".$password."') WHERE id = '".intval($_GET['bal'])."'");
								$error = 2;
							}
							else {
								$error = 1;
							}						
						}
						if(isset($error)) {
						$tpl->assign(array(
							"load" => "new Effect.Pulsate(document.getElementById('error'))",
							"error" => $error
						));
						}
					}
					else {
						header("Location:/panel/hebergement-mails.html?id=".intval($_GET['id']));
					}
				}
				else {
					header("Location:/panel/hebergement-mails.html?id=".intval($_GET['id']));
				}
			}
			elseif($_GET['action'] == "del") {
				if(isset($_GET['bal'])) {
					$bal = intval($_GET['bal']);
					query("UPDATE comptes_heberg SET mails = mails-1 WHERE id ='".$infos_heberg['heberg_id']."'");
					query("DELETE FROM hebergdata_bal WHERE id='".$bal."' AND u_nic='".$_SESSION['u_nic']."'");
					header("Location:/panel/hebergement-mails.html?id=".intval($_GET['id']));
				}
				elseif(isset($_GET['am'])) {
					$am = intval($_GET['am']);
					query("UPDATE comptes_heberg SET alias_mails=alias_mails-1 WHERE id ='".$infos_heberg['heberg_id']."'");
					query("DELETE FROM hebergdata_aliasm WHERE id='".$am."' AND u_nic='".$_SESSION['u_nic']."'");
					header("Location:/panel/hebergement-mails.html?id=".intval($_GET['id']));
				
				}			
			}
			elseif($_GET['action'] == "add_amail") {
				if($infos_heberg['c_a_mails'] < $infos_heberg['o_a_mails']) {
					if(isset($_POST['mail_user'])) {
						$error = 0;
						$domaine = intval($_POST['mail_domain']);
						if(preg_match('#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,5}$#', $_POST['destination'])) {
							if($domaine == 0) {
								if($infos_heberg['dom_lie'] != 0) {
									$mail_dom = $infos_heberg['domaine'];
								}
								else {
									$mail_dom = $infos_heberg['user_linux'].".breizh-web.net";
								}
							}
							else {
								$domaine_pro = query("SELECT * FROM hebergdata_doms WHERE id = '".$domaine."' AND u_nic = '".$_SESSION['u_nic']."'");
								if(mysql_num_rows($domaine_pro) == 1) {
									$domaine_pro = mysql_fetch_array($domaine_pro);
									if($domaine_pro['id_alias_bh'] == 0) {
										$mail_dom = $domaine_pro['domaine'];
									}
									else {
										$mail_dom = $domaine_pro['domaine'].".".$alias_bh[$domaine_pro['id_alias_bh']];
									}
								}
								else {
									$error = 2;
								}
							}
							if($mail_dom != "") {
								$password = mysql_real_escape_string($_POST['password1']);
								$adresse_mail = preg_replace('#([^a-z0-9\-\_/])+#i', '', strtolower($_POST['mail_user']))."@".$mail_dom;
								$mail_exi = mysql_fetch_array(query("SELECT COUNT(*) AS exi FROM hebergdata_aliasm WHERE source = '".$adresse_mail."'"));
								if($mail_exi['exi'] == 0) {
									query("INSERT INTO hebergdata_aliasm(source, destination, u_nic,id_heberg) VALUES('".$adresse_mail."', '".$_POST['destination']."', '".$_SESSION['u_nic']."', '".$infos_heberg['heberg_id']."')");
									query("UPDATE comptes_heberg SET alias_mails = alias_mails + 1 WHERE id  = '".$infos_heberg['heberg_id']."'");
									$error = 3;
								}
								else {
									$error = 2;
								}
							}
						
						}
						else {
							$error = 1;
						}
						
						
					}
					
					$parse = 1;
					$title = "Ajouter un alias mail";
					$tplinc = "panel-hebergement-mails-addam";
					$liste_dom = query("SELECT * FROM hebergdata_doms WHERE u_nic = '".$_SESSION['u_nic']."' AND id != '".$infos_heberg['dom_lie']."'");
					While($doms = mysql_fetch_array($liste_dom)) {
						if($doms['id_alias_bh'] == 0) {
							$tpl->assign_array('liste_dom', array(
								'id' => $doms['id'],
								'domaine' => $doms['domaine'],
							));
						}
						else {
							$tpl->assign_array('liste_ad', array(
								'id' => $doms['id'],
								'domaine' => $doms['domaine'].'.'.$alias_bh[$doms['id_alias_bh']],
							));
						}
					}
					if($infos_heberg['dom_lie'] != 0) {
						$tpl->assign(array('domaine_principal' => $infos_heberg['domaine']));
					}
					else {
						$tpl->assign(array('domaine_principal' => $infos_heberg['user_linux'].".breizh-web.net"));
					}
					if(isset($error)) {
						$tpl->assign(array(
							"load" => "new Effect.Pulsate(document.getElementById('error'))",
							"error" => $error
						));
					}
				}
				else {
					header("Location:/panel/hebergement-mails.html?id=".intval($_GET['id']));
				}
			
			}
			else {
				$parse = 1;
				$tpl->assign(array(
					'user_linux' => $infos_heberg['user_linux'],
					'erreur' => 1,
					'c_bal' => $infos_heberg['c_bal'],
					'o_bal' => $infos_heberg['o_bal'],
					'c_a_mails' => $infos_heberg['c_a_mails'],
					'o_a_mails' => $infos_heberg['o_a_mails'],
					'webmail' => $bzh_config['webmail']
				));
				
				$title = "Gestion des boites aux lettre et alias mails";
				$tplinc = "panel-hebergement-mails";
				$liste_bal = query("SELECT * FROM hebergdata_bal WHERE u_nic = '".$_SESSION['u_nic']."' AND id_heberg = '".$infos_heberg['heberg_id']."'");
				While($bal = mysql_fetch_array($liste_bal)) {
					$tpl->assign_array('bal',array(
						'email' => $bal['email'],
						'id' => $bal['id'],
					));
				}
				$alias_m = query("SELECT * FROM hebergdata_aliasm WHERE u_nic = '".$_SESSION['u_nic']."' AND id_heberg = '".$infos_heberg['heberg_id']."'");
				While($am = mysql_fetch_array($alias_m)) {
					$tpl->assign_array('am',array(
						'email' => $am['source'],
						'destination' => $am['destination'],
						'id' => $am['id'],
					));
				}
			}
		}
		else { header("Location:/panel/hebergement.html?id=".intval($_GET['id'])); }
	}
}
else { header("Location:/site/connexion.html"); }

?>